#include "colorpicker.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QFont>
#include <QFontMetrics>
#include <QDebug>
#include <QDateTime>

CColorPicker::CColorPicker(QWidget *parent)
    : QWidget(parent)
{
    // 设置窗口属性
    //setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
    //setAttribute(Qt::WA_TranslucentBackground);
    setMouseTracking(true);
    //setWindowState(Qt::WindowFullScreen);
    //setCursor(Qt::CrossCursor);

    // 捕获屏幕
    //captureScreen();

    // 初始化历史记录
    m_colorHistory.reserve(HISTORY_SIZE);

    //窗口大小是放大镜大小加上颜色信息大小
    setFixedSize(MAGNIFIER_SIZE, MAGNIFIER_SIZE + COLORINFO_HEIGHT);
}

QColor CColorPicker::getColor()
{
    return m_currentColor;
}

void CColorPicker::Move(const QPoint &pos, PosQuadrant quadrant)
{
    switch (quadrant) {
    case PosQuadrant::rightTop:
        move(pos.x() + 5, pos.y() - 5 - height());
        break;
    case PosQuadrant::rightBottom:
        move(pos.x() + 5, pos.y() + 5);
        break;
    case PosQuadrant::leftTop:
        move(pos.x() - 5 - width(), pos.y() - 5 - height());
        break;
    case PosQuadrant::leftBottom:
        move(pos.x() - 5 - width(), pos.y() + 5);
        break;
    }

    m_cursorPos = pos;
    //取色
    m_currentColor = m_screenCaptureImage.pixelColor(m_cursorPos);
}

void CColorPicker::SetScreenCapture(const QPixmap &pixmap)
{
    m_screenCapture = pixmap;
    m_screenCaptureImage = pixmap.toImage();
}

void CColorPicker::captureScreen()
{
//    QScreen *screen = QGuiApplication::primaryScreen();
//    if (const QWindow *window = windowHandle())
//        screen = window->screen();

//    m_screenCapture = screen->grabWindow(QApplication::desktop()->winId());
}

void CColorPicker::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制屏幕截图背景
    //painter.drawPixmap(0, 0, m_screenCapture);

    // 绘制半透明遮罩
    //painter.fillRect(rect(), QColor(0, 0, 0, 100));

    // 绘制放大镜
    drawMagnifier(painter);

    // 绘制颜色信息
    drawColorInfo(painter);

    // 绘制历史记录
    //drawHistory(painter);
}

void CColorPicker::drawMagnifier(QPainter &painter)
{
    // 计算放大区域
    int halfSize = MAGNIFIER_SIZE / 2;
    QRect sourceRect(m_cursorPos.x() - halfSize / MAGNIFICATION,
                     m_cursorPos.y() - halfSize / MAGNIFICATION,
                     MAGNIFIER_SIZE / MAGNIFICATION,
                     MAGNIFIER_SIZE / MAGNIFICATION);

    // 确保区域在屏幕内
//    if (sourceRect.left() < 0) sourceRect.moveLeft(0);
//    if (sourceRect.top() < 0) sourceRect.moveTop(0);
//    if (sourceRect.right() > m_screenCapture.width()) sourceRect.moveRight(m_screenCapture.width());
//    if (sourceRect.bottom() > m_screenCapture.height()) sourceRect.moveBottom(m_screenCapture.height());

    // 绘制放大图像
    QRect targetRect(/*m_cursorPos.x() + 20*/0, /*m_cursorPos.y() + 20*/0, MAGNIFIER_SIZE, MAGNIFIER_SIZE);
    painter.drawPixmap(targetRect, m_screenCapture, sourceRect);

    int nSide = 8;
    //中心点
    QPoint center(MAGNIFIER_SIZE/2, MAGNIFIER_SIZE/2);
    // 绘制网格线
    painter.setPen(QPen(QColor(0, 0, 255, 80), nSide, Qt::SolidLine));
//    for (int i = 1; i < MAGNIFICATION; ++i) {
//        int pos = i * ((MAGNIFIER_SIZE/ 2) / MAGNIFICATION);
//        painter.drawLine(targetRect.left(), targetRect.top() + pos,
//                         targetRect.right(), targetRect.top() + pos);
//        painter.drawLine(targetRect.left() + pos, targetRect.top(),
//                         targetRect.left() + pos, targetRect.bottom());
//    }
    const int nHalfSide = nSide / 2;
    painter.drawLine(0, center.y()+nHalfSide, center.y()-nHalfSide, center.y()+nHalfSide);//横向第一段
    painter.drawLine(center.x()+nSide+nHalfSide, center.y()+nHalfSide, MAGNIFIER_SIZE, center.y()+nHalfSide);//横向第二段
    painter.drawLine(center.x()+nHalfSide, 0, center.x()+nHalfSide, center.y()-nHalfSide);//纵向第一段 上->下
    painter.drawLine(center.x()+nHalfSide, center.y()+nSide+nHalfSide, center.x()+nHalfSide, MAGNIFIER_SIZE);//纵向第二段

    // 绘制中心十字线
//    painter.setPen(QPen(Qt::red, 2));
//    int centerX = targetRect.left() + halfSize;
//    int centerY = targetRect.top() + halfSize;
//    painter.drawLine(centerX - 10, centerY, centerX + 10, centerY);
//    painter.drawLine(centerX, centerY - 10, centerX, centerY + 10);

    // 绘制边框
    // painter.setPen(QPen(Qt::black, 2));
    // painter.drawRect(targetRect.adjusted(1, 1, -1, -1));
}

void CColorPicker::drawColorInfo(QPainter &painter)
{
    if (m_currentColor.isValid() == false) {
        return;
    }
    //颜色信息大小为MAGNIFIER_SIZExCOLORINFO_HEIGHT
    QRect infoRect(0, MAGNIFIER_SIZE, MAGNIFIER_SIZE, COLORINFO_HEIGHT);

    // 绘制背景
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(30, 30, 30, 220));
    painter.drawRoundedRect(infoRect, 5, 5);

    // 绘制当前颜色块
    QRect colorRect(infoRect.left() + 5, infoRect.top() + 10, 30, 30);
    painter.setBrush(m_currentColor);
    painter.setPen(QPen(Qt::white, 1));
    painter.drawRect(colorRect);

    // 绘制颜色值信息
    painter.setPen(Qt::white);
    QFont font("Arial", 10);
    painter.setFont(font);

    QString hexText = m_currentColor.name().toUpper();
    QString rgbText = QString("RGB(%1, %2, %3)")
                     .arg(m_currentColor.red())
                     .arg(m_currentColor.green())
                     .arg(m_currentColor.blue());

    painter.drawText(infoRect.left() + 40, infoRect.top() + 20, hexText);
    painter.drawText(infoRect.left() + 40, infoRect.top() + 40, rgbText);
}

void CColorPicker::drawHistory(QPainter &painter)
{
    if (m_colorHistory.isEmpty()) return;

    int startX = m_cursorPos.x() - 200;
    int startY = m_cursorPos.y() + 100;
    int boxSize = 30;
    int spacing = 5;

    painter.setPen(Qt::white);
    painter.drawText(startX, startY - 20, "历史颜色:");

    for (int i = 0; i < m_colorHistory.size(); ++i) {
        int x = startX + i * (boxSize + spacing);
        QRect rect(x, startY, boxSize, boxSize);

        // 绘制颜色块
        painter.setBrush(m_colorHistory[i]);
        painter.setPen(QPen(Qt::white, 1));
        painter.drawRect(rect);

        // 绘制索引
        painter.setPen(Qt::white);
        painter.drawText(rect, Qt::AlignCenter, QString::number(i+1));
    }
}

void CColorPicker::addToHistory(const QColor &color)
{
    // 避免重复添加相同颜色
    for (const QColor &c : m_colorHistory) {
        if (c == color) return;
    }

    // 添加到历史记录
    m_colorHistory.prepend(color);

    // 保持历史记录数量
    if (m_colorHistory.size() > HISTORY_SIZE) {
        m_colorHistory.removeLast();
    }
}
